<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent v-if="false">
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="任务ID:" prop="task_id.value">
                            <el-input v-model="queryParams.task_id.value" placeholder="请输入任务ID" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="false"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-tickets"
                                       @click="handleMap(scope.row)">地图</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>
        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen" :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData" :disabled="disabled" @submitForm="submitForm"
                      :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>
        <el-dialog title="查看地图" :visible.sync="mapVisible" :before-close="()=> { mapVisible = false }" destroy-on-close append-to-body>
            <div id="mpContainer" style="width:100%; height: 60vh;"></div>
        </el-dialog>
    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
import { TMap } from '@/utils/TMap'

export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    props: {
        queryObj: {
            type: Object,
            default: ()=> {
                return {
                    task_id: ''
                }
            }
        }
    },
    data() {
        return {
            // table结构
            columns: [
                {
                    visible: true,
                    label: '编号',
                    prop: 'id',
                    width: 60
                },
                {
                    visible: true,
                    label: '类型',
                    prop: 'type',
                    width: 120,
                    component:'QuickAdminTextColor',
                    formatter: (prop, row) => {
                        let type = ''
                        switch (prop) {
                            case 1:
                                type = 'success'
                                break
                            case 2:
                                type = 'warning'
                                break
                            case 3:
                                type = 'info'
                                break
                            case 4:
                                type = 'primary'
                                break

                        }
                        return {
                            value: prop,
                            type: type,
                            text: row.type_text,
                        }
                    },
                },
                {
                    visible: true,
                    label: '所在地区',
                    prop: 'province',
                    overflow: true,
                    component: "QuickAdminText",
                    formatter: (prop, row)=> {
                        return row.province + row.city + row.district + row.address
                    }
                },
                {
                    visible: true,
                    label: '创建日期',
                    prop: 'create_time',
                },
            ],
            // 接口地址
            api: {
                index: `/admin/pz.task_location/index`,
                find: `/admin/pz.task_location/find`,
                add: `/admin/pz.task_location/add`,
                edit: `/admin/pz.task_location/edit`,
                delete: `/admin/pz.task_location/delete`,
                export: `/admin/pz.task_location/export`,
                status: `/admin/pz.task_location/status`
            },
            editFormParams: {},
            // 搜索表单是否展开
            searchExpand: false,
            // 查询参数
            queryParams: {
                task_id: { value:undefined, op: '='},
            },
            type:[
               { label: "发布任务", value: 1 },
               { label: "合作方接单", value: 2 },
               { label: "开始服务", value: 3 },
               { label: "结束服务", value: 4 },
            ],
            tableIsJoinLoad: false,
            mapVisible: false,
            getAddress: {}
        }
    },
    created() {
        if (this.queryObj.task_id)
            this.$set(this.queryParams.task_id, 'value', this.queryObj.task_id)
        this.initIndex()
    },
    methods: {
        handleMap(row) {
            this.initMap(row.latitude, row.longitude, row.address)
            this.mapVisible = true
        },
        // 初始化地图
        initMap(lat, lng, address) {
            TMap('TNVBZ-LXCC3-COB32-O4XES-DHTSE-MCFJX').then(qq => {
                var position = new qq.maps.LatLng(lat, lng)
                var map = new qq.maps.Map(document.getElementById("mpContainer"), {
                    // 地图的中心地理坐标。
                    center: position,
                    zoom: 15
                });
                var mapMarker = new qq.maps.Marker({
                    map: map,
                    position
                })
                var infoWin = new qq.maps.InfoWindow({
                    map: map
                });
                qq.maps.event.addListener(mapMarker, 'click', function() {
                    infoWin.open();
                    infoWin.setContent('<div style="text-align:center;white-space:'+
                        'nowrap;margin:10px;"> ' + address + ' </div>');
                    //提示窗位置
                    infoWin.setPosition(position);
                })
            });
        },
    }
}
</script>
